/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.finalist.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.finalist.dao.BossAreaDao;
import com.finalist.entity.BossArea;
import com.finalist.entity.BossArea.Type;
import com.finalist.service.BossAreaService;

/**
 * Service - 地区
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Service("bossAreaServiceImpl")
public class BossAreaServiceImpl extends BaseServiceImpl<BossArea, Long> implements BossAreaService
{

    @Resource(name = "bossAreaDaoImpl")
    private BossAreaDao bossAreaDao;

    @Resource(name = "bossAreaDaoImpl")
    public void setBaseDao(BossAreaDao bossAreaDao)
    {
        super.setBaseDao(bossAreaDao);
    }

    @Transactional(readOnly = true)
    public List<BossArea> findRoots()
    {
        return bossAreaDao.findRoots(null);
    }

    @Transactional(readOnly = true)
    public List<BossArea> findRoots(Integer count)
    {
        return bossAreaDao.findRoots(count);
    }

    @Override
    @Transactional
    @CacheEvict(value = "bossArea", allEntries = true)
    public void save(BossArea area)
    {
        super.save(area);
    }

    @Override
    @Transactional
    @CacheEvict(value = "bossArea", allEntries = true)
    public BossArea update(BossArea area)
    {
        return super.update(area);
    }

    @Override
    @Transactional
    @CacheEvict(value = "bossArea", allEntries = true)
    public BossArea update(BossArea area, String... ignoreProperties)
    {
        return super.update(area, ignoreProperties);
    }

    @Override
    @Transactional
    @CacheEvict(value = "bossArea", allEntries = true)
    public void delete(Long id)
    {
        super.delete(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = "bossArea", allEntries = true)
    public void delete(Long... ids)
    {
        super.delete(ids);
    }

    @Override
    @Transactional
    @CacheEvict(value = "bossArea", allEntries = true)
    public void delete(BossArea area)
    {
        super.delete(area);
    }

    @Override
    public List<BossArea> findPrefixByName(String cityName)
    {
        return bossAreaDao.findPrefixByCityName(cityName);
    }
    @Override
    public BossArea findByNoPrefix(String noPrefix)
    {
        return bossAreaDao.findByNoPrefix(noPrefix);
    }
    @Override
    public BossArea findInfoOfNoPrefix(String noPrefix, Type noPrefixType)
    {
        return bossAreaDao.findInfoOfNoPrefix(noPrefix,noPrefixType);
    }   
    @Override
    public BossArea findByPrefix(String prefix)
    {
        return bossAreaDao.findByPrefix(prefix);
    }

    @Override
	public BossArea findCodeBySelectedCity(String selectedCity, BossArea.Type type) {
		return bossAreaDao.findCodeBySelectedCity(selectedCity, type);
	}  

    @Override
    public BossArea findCityByCityCode(String cityCode,BossArea.Type type)
    {
        return bossAreaDao.findCityByCityCode(cityCode, type);
    }

    
    @Override
    public BossArea findByParentId(Long parentId)
    {
        return bossAreaDao.findByParentId(parentId);
    }

    @Override
    public List<BossArea> findAllProvince()
    {
        return bossAreaDao.findByAllProvince();
    }

    @Override
    public String findCodeByCityName(String cityName)
    {
        return bossAreaDao.findCodeByCityName(cityName);
    }

    @Override
    public String findCodeByName(String cityName)
    {
        return bossAreaDao.findCodeByName(cityName);
    }
    
    @Override
    @Transactional
    public String saveSectionInfo(List<BossArea> bossAreaList)
    {
        for (BossArea bossArea : bossAreaList)
        {
            BossArea parentBossArea = bossAreaDao.find(bossArea.getParent().getId());
            bossArea.setParent(parentBossArea);
            BossArea tempBossArea = bossAreaDao.findByPrefix(bossArea.getNoPrefix());
            if (tempBossArea != null)
            {
                continue;
            }
            bossAreaDao.merge(bossArea);
        }
        return "success";
    }

    @Override
    public List<BossArea> findBossAreaByType(Type type)
    {
        return bossAreaDao.findBossAreaByType(type);
    }
    
    /**
     * 根据cityCode查找地区
     */
    public List<BossArea> findBossAreaByCityCode(String cityCode){
    	return bossAreaDao.findBossAreaByCityCode(cityCode);
    }

	@Override
	public BossArea saveBossArea(BossArea bossArea) {
		return bossAreaDao.merge(bossArea);
	}
	/**
     * 根据号段查找地区判断是否是风客旅行用户
     */
	@Override
	public BossArea findByPrefix(String prefix, Boolean canCharge) {
		return bossAreaDao.findByPrefix(prefix, canCharge);
	} 

}